package com.java.Jdbc;

import java.sql.*;

/**
 * DriverManagerDemo演示了如何使用DriverManager连接到MySQL数据库并创建Statement
 */
public class PreparedStatementDemo {

    public static final String url = "jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8&useSSL=false";
    public static final String username = "root";
    public static final String password = "root";

    public static void main(String[] args) {
        // 创建连接
        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            System.out.println(connection);
            // 获得预编译对象
            PreparedStatement preparedStatement = connection.prepareStatement(
                    "insert into product(name,price,address,type) values (?,?,?,?)");
            preparedStatement.setObject(1, "三星手机");
            preparedStatement.setObject(2, 5800);
            preparedStatement.setObject(3, "吉林");
            preparedStatement.setObject(4, "手机");

            //执行SQL语句
            int rs = preparedStatement.executeUpdate();
            System.out.println("受影响的行数：" + rs);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
